package com.eight.paper.admin.controller;

import com.alibaba.fastjson2.JSONObject;
import com.eight.common.constant.ExamConstants;
import com.eight.common.core.controller.BaseController;
import com.eight.common.core.domain.AjaxResult;
import com.eight.common.core.page.TableDataInfo;
import com.eight.common.core.redis.RedisCache;
import com.eight.common.utils.StringUtils;
import com.eight.paper.admin.domain.ExamPaper;
import com.eight.paper.admin.domain.ExamPaperExaminee;
import com.eight.paper.admin.domain.model.ExamPaperExamineeResponse;
import com.eight.paper.admin.domain.model.ExamPaperReponseBody;
import com.eight.paper.admin.domain.model.ExamineeMarkQueue;
import com.eight.paper.admin.domain.model.enums.AnswerShowTypeEnum;
import com.eight.paper.admin.service.IExamPaperExamineeService;
import com.eight.paper.admin.service.IExamPaperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 考试阅卷
 * 
 * @author eight
 * @date 2023-10-31
 */
@Api(value = "考试阅卷", tags = "考试阅卷")
@RestController
@RequestMapping("/paper/mark")
public class ExamMarkController extends BaseController
{
    @Autowired
    private IExamPaperService examPaperService;
    @Autowired
    private IExamPaperExamineeService examPaperExamineeService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询考试试卷列表
     */
    @ApiOperation(value = "查询考试试卷列表")
    @PreAuthorize("@ss.hasPermi('paper:mark:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamPaper examPaper)
    {
        startPage();
        examPaper.setGrades(getUserId().toString());
        List<ExamPaper> list = examPaperService.selectMarkPaperList(examPaper);
        return getDataTable(list);
    }

    /**
     * 查询考生答卷总列表
     */
    @ApiOperation(value = "查询考生答卷总列表")
    @PreAuthorize("@ss.hasPermi('paper:mark:listExaminee')")
    @GetMapping("/listExaminee")
    public TableDataInfo list(ExamPaperExaminee examPaperExaminee)
    {
        startPage();
        List<ExamPaperExamineeResponse> list = examPaperExamineeService.selectExamPaperExamineeList(examPaperExaminee);
        return getDataTable(list);
    }


    /**
     * 获取考生答卷详细信息
     */
    @ApiOperation(value = "获取考生答卷详细信息")
    @PreAuthorize("@ss.hasPermi('paper:mark:getExamineeExamInfo')")
    @GetMapping(value = "/getExamineeExamInfo/{paperId}/{userId}/{examineeId}")
    public AjaxResult getExamineeExamInfo(@PathVariable("paperId") Integer paperId,@PathVariable("userId") Integer userId,@PathVariable("examineeId") Integer examineeId)
    {
        String key = String.format("%s:%s:%s", ExamConstants.TEMP_MARK,paperId,userId);
        if(redisCache.hasKey(key)){
            //return AjaxResult.success((JSONObject)redisCache.getCacheObject(key));
        }
        ExamPaperReponseBody examPaper = examPaperService.selectExamPaperByIdAndUserId(paperId,userId.longValue());
        return AjaxResult.success(examPaper);
       // return success(examPaperService.selectExamPaperById(paperId, AnswerShowTypeEnum.False.getCode()));
    }

    /**
     * 获取下一个考生答卷详细信息
     */
    @ApiOperation(value = "获取下一个考生答卷详细信息")
    @PreAuthorize("@ss.hasPermi('paper:mark:getExamineeExamInfo')")
    @GetMapping(value = "/getNextExamineeExamInfo/{paperId}")
    public AjaxResult getNextExamineeExamInfo(@PathVariable("paperId") Integer paperId)
    {
        String key = String.format("%s:%s", ExamConstants.EXAMINEE_MARK_QUEUE,paperId);
        if(!redisCache.hasKey(key)){
            examPaperService.loadExamineeMarkQueue(paperId);
        }
        Object value = redisCache.getCacheRightPop(key);
        if(StringUtils.isNotNull(value)) {
            ExamineeMarkQueue examinee = (ExamineeMarkQueue) value;
            return AjaxResult.success(examinee);
        }else {
            return AjaxResult.error("已是最后一名考生");
        }
    }

    /**
     * 提交阅卷
     */
    @ApiOperation(value = "提交试卷")
    @PreAuthorize("@ss.hasPermi('paper:mark:submitMark')")
    @PostMapping(value = "/submitMark/{paperId}/{userId}/{examineeId}")
    public AjaxResult submitMark(@PathVariable("paperId") Integer paperId,@PathVariable("userId") Integer userId,@PathVariable("examineeId") Integer examineeId,@RequestBody JSONObject paper)
    {
        examPaperService.tempMark(paperId,userId,paper);
        examPaperService.submitMark(paperId,examineeId,paper,null);
        return success();
    }

    /**
     * 暂存阅卷
     */
    @ApiOperation(value = "暂存试卷")
    @PreAuthorize("@ss.hasPermi('paper:mark:tempMark')")
    @PostMapping(value = "/tempMark/{paperId}/{examineeId}")
    public AjaxResult tempMark(@PathVariable("paperId") Integer paperId,@PathVariable("userId") Integer userId,@RequestBody JSONObject paper)
    {
        examPaperService.tempMark(paperId,userId,paper);
        return success();
    }

}
